﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace WcfClientGenerator.UI.Converters
{
    public class StringListStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = null;
            List<string> list = value as List<string>;
            if (list != null)
            {
                StringBuilder builder = new StringBuilder();
                foreach (string item in list)
                {
                    builder.AppendFormat(builder.Length > 0 ? ";{0}" : "{0}", item);
                }
                result = builder.ToString();
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<string> list = null;
            string stringList = value as string;
            if (stringList != null)
            {
                string[] parts = stringList.Split(';');
                list = new List<string>(parts);
            }

            return list;
        }
    }
}
